package mall

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"newbeemallStudyProject/global"
	"newbeemallStudyProject/model/common/response"
	"newbeemallStudyProject/model/mall/request"
	"newbeemallStudyProject/service"
)

type MallUserApi struct{}

func (m *MallUserApi) GetTest(c *gin.Context) {
	fmt.Println("进入到api逻辑层即将进入service层===")
	mallUserService.TestService()
	serviceGroup := service.ServiceGroupApp.MallServiceGroup
	fmt.Println(serviceGroup)
}

func (m *MallUserApi) GetUserInfo(c *gin.Context) {
	token := c.GetHeader("token")
	if err, mall := mallUserService.QueryUserInfo(token); err != nil {
		global.GVA_LOG.Error("未查询到记录", zap.Error(err))
		response.FailWithMsg(c, "未查询到记录")
	} else {
		response.OkWithData(c, mall)
	}
	fmt.Println("进来了===")
}

func (m *MallUserApi) UserRegister(c *gin.Context) {
	var userRrgisterParam request.RegisterUserParam
	// json --》 结构体
	_ = c.BindJSON(&userRrgisterParam)
	//if err := utils.Verify(userRrgisterParam, utils.MallUserRegisterVerify); err != nil {
	//	response.FailWithData(c, err)
	//	return
	//}
	if len(userRrgisterParam.LoginName) == 0 {
		response.FailWithMsg(c, "用户名不能为空")
		return
	}
	if len(userRrgisterParam.Password) == 0 {
		response.FailWithMsg(c, "密码不能为空")
		return
	}
	err := mallUserService.RegisterUser(userRrgisterParam)
	if err != nil {
		global.GVA_LOG.Error("用户名重复", zap.Error(err))
		response.FailWithData(c, "用户名不能重复")
	}
}

func (m *MallUserApi) UserLogin(c *gin.Context) {
	var userLogin request.UserLoginParam
	_ = c.BindJSON(&userLogin)
	err, _, token := mallUserService.UserLogin(userLogin)
	if err != nil {
		response.FailWithMsg(c, "登录失败")
	} else {
		response.OkWithData(c, token.Token)
	}

}

func (m *MallUserApi) UserLayout(c *gin.Context) {
	token := c.GetHeader("token")
	err := mallUserService.UserLayout(token)
	if err != nil {
		global.GVA_LOG.Error("登出失败", zap.Error(err))
		response.FailWithMsg(c, "登出失败。。")
	} else {
		response.OkWithData(c, "ok")
	}
}

func (m *MallUserApi) UserInfoUpdate(c *gin.Context) {
	var userUpdatParam request.UserUpdateParam
	c.BindJSON(&userUpdatParam)
	token := c.GetHeader("token")
	err := mallUserService.UserInfoUpdate(userUpdatParam, token)
	if err != nil {
		global.GVA_LOG.Error("修改个人信息失败", zap.Error(err))
		response.FailWithMsg(c, "修改个人信息失败")
	} else {
		response.OkWithData(c, "ok")
	}
}
